# Libraries
library(reshape2)
library(tidyverse)
# Graphs
theme_set (theme_classic() + theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
legend.position="none",
axis.text.x = element_text(angle = 90, vjust = 0.5),
plot.title = element_text(size=12, face="bold"),
#panel.border = element_rect(colour = "black", fill=NA, size=1)
panel.border = element_blank()
))
# Data
# Fragment list
#CurrentFragments<-read.csv("8.Metadata/Frag_assignments.csv", header = T)
# Collection information
#Location<-read.csv("8.Metadata/Genotypes.csv", header = T)
# Treatment Info
Treatment<-read.csv("Data/Treatments.csv")
#duplicated(Treatment$Tag)
Treatment<-Treatment[!duplicated(Treatment$Tag), ]
#duplicated(Treatment$Tag)
summary(Treatment)
## Tag Genotype Nutrients Disease
## 201 : 1 Elkhorn : 28 Ambient:127 Dead : 1
## 202 : 1 Kelsey-1: 26 Dead : 1 Extra : 6
## 204 : 1 U44 : 26 Extra : 6 Pathogen:135
## 205 : 1 Acerv2 : 25 NH4 :126 Placebo :118
## 207 : 1 FM19 : 25 TL : 1 TL : 1
## 208 : 1 FM6 : 25
## (Other):255 (Other) :106
## Available
## Dead : 4
## Experiment:250
## Extra? : 7
##
##
##
##
# Tags weight
Tags<-read.csv("Data/Tags_W.csv")
#duplicated(Tags$Tag)
Tags<-Tags[!duplicated(Tags$Tag), ]
#duplicated(Tags$Tag)
summary(Tags)
## T_Type Tag T_AW T_BW Temperature
## B:200 201 : 1 Min. :10.01 Min. :1.393 Min. :23.33
## S: 75 202 : 1 1st Qu.:10.14 1st Qu.:1.444 1st Qu.:26.18
## 204 : 1 Median :16.26 Median :2.285 Median :26.35
## 205 : 1 Mean :14.60 Mean :2.054 Mean :26.16
## 207 : 1 3rd Qu.:16.31 3rd Qu.:2.298 3rd Qu.:26.82
## 208 : 1 Max. :16.48 Max. :2.333 Max. :27.88
## (Other):269
## Salinity X
## Min. :33.2 :271
## 1st Qu.:33.2 Guessed: 4
## Median :33.2
## Mean :33.2
## 3rd Qu.:33.2
## Max. :33.2
##
Tags$SW_den<-(999.842594+0.06793952*(Tags$Temperature)-0.00909529*(Tags$Temp)^2+0.0001001685*
(Tags$Temperature)^3-0.000001120083*(Tags$Temperature)^4+0.000000006536332*(Tags$Temp)^5+(0.824493-0.0040899*
(Tags$Temperature)+0.000076438*(Tags$Temperature)^2-0.00000082467*(Tags$Temperature)^3+0.0000000053875*
(Tags$Temperature)^4)*(Tags$Salinity)+(-0.00572466+0.00010227*(Tags$Temperature)-0.0000016546*
(Tags$Temperature)^2)* (Tags$Salinity)^1.5+0.00048314*(Tags$Salinity)^2)*0.001
#Tags$Estimated_Density<-(-Tags$SW_den/((Tags$T_BW/Tags$T_AW)-1))
Tags %>%
group_by(T_Type) %>%
summarise_at(vars(T_AW, T_BW), funs(mean(., na.rm=TRUE)))
# 1. BW data
BW_Tall<-read.csv("Data/BW_ Long_data.csv")
#BW_Tall$Estimated.W.density[BW_Tall$Estimated.W.density == "#VALUE!" ] <-NA
#BW_Tall$Estimated.W.density<-as.numeric(as.character(BW_Tall$Estimated.W.density))
#BW_Tall$Estimated_AW [BW_Tall$Estimated_AW == "#VALUE!" ] <-NA
#BW_Tall$Estimated_AW<-as.numeric(as.character(BW_Tall$Estimated_AW))
# 2. Data clean-up an types:
# Variable types
#BW_Tall$Time <- as.factor(BW_Tall$Time)
#BW_Tall$Time<-as.numeric(BW_Tall$Time)
BW_Tall$Date<-as.Date(BW_Tall$Date, "%Y-%m-%d")
BW_Tall$Day<-(as.numeric(BW_Tall$Date)-18486)
# Remove-unused data
#Extras <- BW_Tall[which (BW_Tall$Nutrients=="Extra"), ]
#BW_Tall <- droplevels(BW_Tall[!rownames(BW_Tall) %in%
# rownames(Extras), ])
# 3. Merge with treatments
BW_Tall<-plyr::join(BW_Tall, Treatment, by = "Tag",
type = "left", match = "all")
BW_Tall<-plyr::join(BW_Tall, Tags, by = "Tag",
type = "left", match = "all")
BW_Tall$Nutrients<-factor(BW_Tall$Nutrients,
levels= c("Ambient", "NH4"), ordered=TRUE)
BW_Tall$Disease<-factor(BW_Tall$Disease,
levels= c("Placebo", "Pathogen"), ordered=TRUE)
# 4. Replicates
BW_Tall$Tank<-factor(BW_Tall$Tank, ordered=FALSE)
BW_Tall$Genotype<-factor(BW_Tall$Genotype, ordered=FALSE)
summary(BW_Tall)
## Tag Tank Date BW
## 201 : 4 3 :125 Min. :2020-08-12 Min. : 2.515
## 202 : 4 4 :125 1st Qu.:2020-08-12 1st Qu.: 3.923
## 204 : 4 6 :125 Median :2020-08-21 Median : 4.652
## 205 : 4 1 :124 Mean :2020-08-27 Mean : 5.183
## 207 : 4 7 :124 3rd Qu.:2020-09-04 3rd Qu.: 6.194
## 208 : 4 8 :124 Max. :2020-09-11 Max. :11.950
## (Other):985 (Other):262
## Temp Sal Notes
## Min. :26.50 Min. :32.10 :984
## 1st Qu.:27.30 1st Qu.:32.50 new to the experiment: 6
## Median :27.50 Median :34.00 New : 5
## Mean :27.58 Mean :33.71 Batch 2 : 1
## 3rd Qu.:27.90 3rd Qu.:34.40 dead : 1
## Max. :29.10 Max. :34.70 maybe broke at top : 1
## (Other) : 11
## Day Genotype Nutrients Disease
## Min. : 0.00 Elkhorn :103 Ambient:496 Placebo :467
## 1st Qu.: 0.00 Kelsey-1: 98 NH4 :501 Pathogen:530
## Median : 9.00 U44 : 98 NA's : 12 NA's : 12
## Mean :15.46 FM19 : 97
## 3rd Qu.:23.00 FM6 : 97
## Max. :30.00 Acerv2 : 96
## (Other) :420
## Available T_Type T_AW T_BW Temperature
## Dead : 10 B:724 Min. :10.01 Min. :1.393 Min. :23.33
## Experiment:988 S:285 1st Qu.:10.14 1st Qu.:1.443 1st Qu.:26.09
## Extra? : 11 Median :16.26 Median :2.284 Median :26.34
## Mean :14.53 Mean :2.046 Mean :26.13
## 3rd Qu.:16.31 3rd Qu.:2.298 3rd Qu.:26.84
## Max. :16.48 Max. :2.333 Max. :27.88
##
## Salinity X SW_den
## Min. :33.2 :1005 Min. :1.021
## 1st Qu.:33.2 Guessed: 4 1st Qu.:1.021
## Median :33.2 Median :1.022
## Mean :33.2 Mean :1.022
## 3rd Qu.:33.2 3rd Qu.:1.022
## Max. :33.2 Max. :1.022
##
BW.data<-BW_Tall[order(BW_Tall$Tag), ]
# 3. Calculate days bw BW data points
BW.data<-BW.data %>%
group_by(Tag) %>%
dplyr::mutate(Days = Day - lag(Day, default = Day[1]))
# 4. Calculate change in BW bw data points
BW.data<-BW.data %>%
group_by(Tag) %>%
dplyr::mutate(dBW = BW - lag(BW, default = BW[1]))
BW.data<-BW.data %>%
group_by(Tag) %>%
dplyr::mutate(dBW_r = dBW/(Days*lag(BW, default = BW[1])))
BW.data$dBW_r<-BW.data$dBW_r*1000
summary(BW.data)
## Tag Tank Date BW
## 201 : 4 3 :125 Min. :2020-08-12 Min. : 2.515
## 202 : 4 4 :125 1st Qu.:2020-08-12 1st Qu.: 3.923
## 204 : 4 6 :125 Median :2020-08-21 Median : 4.652
## 205 : 4 1 :124 Mean :2020-08-27 Mean : 5.183
## 207 : 4 7 :124 3rd Qu.:2020-09-04 3rd Qu.: 6.194
## 208 : 4 8 :124 Max. :2020-09-11 Max. :11.950
## (Other):985 (Other):262
## Temp Sal Notes
## Min. :26.50 Min. :32.10 :984
## 1st Qu.:27.30 1st Qu.:32.50 new to the experiment: 6
## Median :27.50 Median :34.00 New : 5
## Mean :27.58 Mean :33.71 Batch 2 : 1
## 3rd Qu.:27.90 3rd Qu.:34.40 dead : 1
## Max. :29.10 Max. :34.70 maybe broke at top : 1
## (Other) : 11
## Day Genotype Nutrients Disease
## Min. : 0.00 Elkhorn :103 Ambient:496 Placebo :467
## 1st Qu.: 0.00 Kelsey-1: 98 NH4 :501 Pathogen:530
## Median : 9.00 U44 : 98 NA's : 12 NA's : 12
## Mean :15.46 FM19 : 97
## 3rd Qu.:23.00 FM6 : 97
## Max. :30.00 Acerv2 : 96
## (Other) :420
## Available T_Type T_AW T_BW Temperature
## Dead : 10 B:724 Min. :10.01 Min. :1.393 Min. :23.33
## Experiment:988 S:285 1st Qu.:10.14 1st Qu.:1.443 1st Qu.:26.09
## Extra? : 11 Median :16.26 Median :2.284 Median :26.34
## Mean :14.53 Mean :2.046 Mean :26.13
## 3rd Qu.:16.31 3rd Qu.:2.298 3rd Qu.:26.84
## Max. :16.48 Max. :2.333 Max. :27.88
##
## Salinity X SW_den Days
## Min. :33.2 :1005 Min. :1.021 Min. : 0.000
## 1st Qu.:33.2 Guessed: 4 1st Qu.:1.021 1st Qu.: 0.000
## Median :33.2 Median :1.022 Median : 7.000
## Mean :33.2 Mean :1.022 Mean : 7.431
## 3rd Qu.:33.2 3rd Qu.:1.022 3rd Qu.: 9.000
## Max. :33.2 Max. :1.022 Max. :14.000
##
## dBW dBW_r
## Min. :-4.92280 Min. :-86.899
## 1st Qu.: 0.00000 1st Qu.: 1.035
## Median : 0.05050 Median : 1.607
## Mean : 0.05834 Mean : 1.514
## 3rd Qu.: 0.09370 3rd Qu.: 2.037
## Max. : 0.92380 Max. : 41.867
## NA's :261
SizeBiasCheck<- ggplot(BW.data, aes (BW, dBW_r, colour=factor(Genotype))) +
#geom_smooth(method = "lm")+
geom_jitter(alpha=0.5) +
scale_y_continuous(limits = c(0, 5),
breaks = seq(0, 6,2),
expand = c(0, 0),
name=("BW mg / g*day"))
SizeBiasCheck
BW_Genet<- ggplot(BW.data, aes (Genotype, dBW_r, colour=factor(Date))) +
stat_summary(fun.data = "mean_cl_boot",geom = "errorbar", width = 0.2 )+
stat_summary(fun.y=mean, geom="line") +
geom_jitter(alpha=0.5) +
scale_y_continuous(limits = c(0, 5),
breaks = seq(0, 5,1),
expand = c(0, 0),
name=("dBW [mg / g*day]"))+
theme(legend.position = "bottom")
BW_Genet
BW_Genet+ facet_wrap(~Nutrients)
BW_Genet+ facet_wrap(~Disease)
#BW_Genet+ facet_wrap(Nutrients~Disease)
BW_Bias<- ggplot(BW.data, aes (Genotype, dBW_r, colour=factor(Nutrients))) +
stat_summary(fun.data = "mean_cl_boot",geom = "errorbar", width = 0.2 )+
stat_summary(fun.y=mean, geom="line") +
geom_jitter(alpha=0.5) +
scale_y_continuous(limits = c(0, 5),
breaks = seq(0, 5,1),
expand = c(0, 0),
name=("dBW [mg / g*day]"))
BW_Bias + facet_grid(Date~Disease)
# 1. Coral BW
BW.data$Coral_BW<-(BW.data$BW)-(BW.data$T_BW)
# 2. Water density
BW.data$SW_den<-(999.842594+0.06793952*(BW.data$Temp)-0.00909529*(BW.data$Temp)^2+0.0001001685*
(BW.data$Temp)^3-0.000001120083*(BW.data$Temp)^4+0.000000006536332*(BW.data$Temp)^5+(0.824493-0.0040899*
(BW.data$Temp)+0.000076438*(BW.data$Temp)^2-0.00000082467*(BW.data$Temp)^3+0.0000000053875*(BW.data$Temp)^4)*
(BW.data$Salinity)+(-0.00572466+0.00010227*(BW.data$Temp)-0.0000016546*(BW.data$Temp)^2)*
(BW.data$Salinity)^1.5+0.00048314*(BW.data$Salinity)^2)*0.001
# 3. Coral air weight
BW.data$Est_Coral_AW<-(BW.data$Coral_BW)*(1/(1-(BW.data$SW_den)/2.4))
# 4. Calculate change in AW bw data points
BW.data<-BW.data %>%
dplyr::group_by(Tag) %>%
dplyr::mutate(dAW = Est_Coral_AW - lag(Est_Coral_AW, default = Est_Coral_AW[1]))
BW.data<-BW.data %>%
group_by(Tag) %>%
dplyr::mutate(dAW_r = dAW/(Days*lag(Est_Coral_AW, default = Est_Coral_AW[1])))
BW.data$dAW_r<-BW.data$dAW_r*1000
summary(BW.data)
## Tag Tank Date BW
## 201 : 4 3 :125 Min. :2020-08-12 Min. : 2.515
## 202 : 4 4 :125 1st Qu.:2020-08-12 1st Qu.: 3.923
## 204 : 4 6 :125 Median :2020-08-21 Median : 4.652
## 205 : 4 1 :124 Mean :2020-08-27 Mean : 5.183
## 207 : 4 7 :124 3rd Qu.:2020-09-04 3rd Qu.: 6.194
## 208 : 4 8 :124 Max. :2020-09-11 Max. :11.950
## (Other):985 (Other):262
## Temp Sal Notes
## Min. :26.50 Min. :32.10 :984
## 1st Qu.:27.30 1st Qu.:32.50 new to the experiment: 6
## Median :27.50 Median :34.00 New : 5
## Mean :27.58 Mean :33.71 Batch 2 : 1
## 3rd Qu.:27.90 3rd Qu.:34.40 dead : 1
## Max. :29.10 Max. :34.70 maybe broke at top : 1
## (Other) : 11
## Day Genotype Nutrients Disease
## Min. : 0.00 Elkhorn :103 Ambient:496 Placebo :467
## 1st Qu.: 0.00 Kelsey-1: 98 NH4 :501 Pathogen:530
## Median : 9.00 U44 : 98 NA's : 12 NA's : 12
## Mean :15.46 FM19 : 97
## 3rd Qu.:23.00 FM6 : 97
## Max. :30.00 Acerv2 : 96
## (Other) :420
## Available T_Type T_AW T_BW Temperature
## Dead : 10 B:724 Min. :10.01 Min. :1.393 Min. :23.33
## Experiment:988 S:285 1st Qu.:10.14 1st Qu.:1.443 1st Qu.:26.09
## Extra? : 11 Median :16.26 Median :2.284 Median :26.34
## Mean :14.53 Mean :2.046 Mean :26.13
## 3rd Qu.:16.31 3rd Qu.:2.298 3rd Qu.:26.84
## Max. :16.48 Max. :2.333 Max. :27.88
##
## Salinity X SW_den Days
## Min. :33.2 :1005 Min. :1.021 Min. : 0.000
## 1st Qu.:33.2 Guessed: 4 1st Qu.:1.021 1st Qu.: 0.000
## Median :33.2 Median :1.021 Median : 7.000
## Mean :33.2 Mean :1.021 Mean : 7.431
## 3rd Qu.:33.2 3rd Qu.:1.021 3rd Qu.: 9.000
## Max. :33.2 Max. :1.022 Max. :14.000
##
## dBW dBW_r Coral_BW Est_Coral_AW
## Min. :-4.92280 Min. :-86.899 Min. : 0.433 Min. : 0.7537
## 1st Qu.: 0.00000 1st Qu.: 1.035 1st Qu.: 1.681 1st Qu.: 2.9269
## Median : 0.05050 Median : 1.607 Median : 2.510 Median : 4.3683
## Mean : 0.05834 Mean : 1.514 Mean : 3.138 Mean : 5.4617
## 3rd Qu.: 0.09370 3rd Qu.: 2.037 3rd Qu.: 4.060 3rd Qu.: 7.0654
## Max. : 0.92380 Max. : 41.867 Max. :10.517 Max. :18.3077
## NA's :261
## dAW dAW_r
## Min. :-8.5682 Min. :-105.341
## 1st Qu.: 0.0000 1st Qu.: 1.976
## Median : 0.0882 Median : 2.746
## Mean : 0.1018 Mean : 3.051
## 3rd Qu.: 0.1635 3rd Qu.: 3.738
## Max. : 1.6120 Max. : 180.134
## NA's :261
BW.CRF<-BW.data[which(BW.data$Genotype=='K2'|
BW.data$Genotype=='U41'|
BW.data$Genotype=='U44'), ]
BW.UM<-BW.data[which(BW.data$Genotype=='Acerv2'|
BW.data$Genotype=='Cooper-9'|
BW.data$Genotype=='Elkhorn'|
BW.data$Genotype=='Kelsey-1'), ]
BW.FWC<-BW.data[which(BW.data$Genotype=='FM14'|
BW.data$Genotype=='FM6'|
BW.data$Genotype=='FM9'), ]
SizeBiasCheck<- ggplot(BW.data, aes (Est_Coral_AW, dAW_r, colour=factor(Genotype))) +
#stat_summary(fun.data = "mean_cl_boot",geom = "errorbar", width = 0.2 )+
#stat_summary(fun.y=mean, geom="line") +
geom_jitter(alpha=0.5) +
scale_y_continuous(limits = c(0, 10),
breaks = seq(0, 10,2),
expand = c(0, 0),
name=("dAW [mg / g*day]"))
SizeBiasCheck
AW_Genet<- ggplot(BW.data, aes (Genotype, dAW_r, colour=factor(Date))) +
stat_summary(fun.data = "mean_cl_boot",geom = "errorbar", width = 0.2 )+
stat_summary(fun.y=mean, geom="line") +
theme(legend.position = "none")+
geom_jitter(alpha=0.5) + theme(legend.position = "bottom")+
scale_y_continuous(limits = c(0, 10),
breaks = seq(0, 8,2),
expand = c(0, 0),
name=("AW mg / g*day"))
AW_Genet
AW_Genet+ facet_wrap(~Nutrients)
AW_Genet+ facet_wrap(~Disease)
AW_Genet2<- ggplot(BW.data, aes (Date, dAW_r, colour=Genotype)) +
stat_summary(fun.data = "mean_cl_boot",geom = "errorbar", width = 0.2 )+
stat_summary(fun.y=mean, geom="line") +
theme(legend.position = "none")+
geom_jitter(shape=21, alpha=0.3)+
scale_y_continuous(limits = c(0, 10),
breaks = seq(0, 10,2),
expand = c(0, 0),
name=("AW mg / g*day"))+
theme(legend.position = "bottom")
AW_Genet2
AW_Genet2 + facet_grid(Nutrients~Disease)
BW_Frag<- ggplot(BW.data, aes (Date, dAW_r, group=(Tag),
colour=factor(Genotype))) +
geom_line()+
geom_hline(yintercept = 0, linetype=3)+
#scale_x_continuous(name="Days",
# breaks = seq(0, 30, by=15)) +
scale_y_continuous(name="Growth (mg / g d)",
limits = c(-2, 10),
breaks = seq(-1, 10, by=1)) +
theme(legend.position="bottom",
legend.title = element_blank(),
strip.background =element_rect(fill=NA))
BW_Frag+facet_wrap(~Genotype)
AW_Tank<- ggplot(BW.data, aes (Tank, dAW_r)) +
stat_summary(fun.data = "mean_cl_boot",geom = "errorbar", width = 0.2 )+
stat_summary(fun.y=mean, geom="line") +
scale_y_continuous(limits = c(0, 10),
breaks = seq(0, 10,2),
expand = c(0, 0),
name=("Growth rate (mg / g day)"))+
geom_jitter(aes(colour=Genotype), shape=21, alpha=0.3)+
facet_wrap(~Date) +
theme(legend.position = "bottom")
AW_Tank
#AW_Tank+ facet_wrap(Date~Nutrients)
#AW_Tank+ facet_wrap(Date~Disease)
AW_Disease<- ggplot(BW.data, aes (Disease, dAW_r)) +
stat_summary(fun.data = "mean_cl_boot",geom = "errorbar", width = 0.2 )+
stat_summary(fun.y=mean, geom="line") +
scale_y_continuous(limits = c(-3, 10),
breaks = seq(-3, 10,2),
expand = c(0, 0),
name=("Growth rate (mg/g day)"))+
geom_jitter(aes(colour=Genotype), alpha=0.3)+
facet_wrap(~Date) +
theme(legend.position = "bottom")
AW_Disease
AW_Disease + facet_wrap(Date~Nutrients)
AW_Disease + facet_wrap(Date~Disease)
AW_Bias<- ggplot(BW.data, aes (Date, dAW_r, colour=factor(Genotype), shape=(Nutrients))) +
stat_summary(fun.data = "mean_cl_boot",geom = "errorbar", width = 0.2 )+
stat_summary(fun.y=mean, geom="line") +
geom_jitter(alpha=0.5) +
scale_y_continuous(limits = c(0, 5),
breaks = seq(0, 5,1),
expand = c(0, 0),
name=("dBW [mg / g*day]"))
AW_Bias + facet_grid(~Disease)+
theme(legend.position = "bottom")
#Summary<-BW.data %>% group_by(Genotype, Nutrients, Disease) %>% count(Date)
#BW_Tall2<-BW.data %>% group_by(Disease, Genotype, Nutrients) %>% add_count(Date)
#write.csv(Summary, "numbers.csv")
# Creates bibliography
#knitr::write_bib(c(.packages()), "packages.bib")
Henry, Lionel, and Hadley Wickham. 2019. Purrr: Functional Programming Tools. https://CRAN.R-project.org/package=purrr.
Müller, Kirill, and Hadley Wickham. 2019. Tibble: Simple Data Frames. https://CRAN.R-project.org/package=tibble.
R Core Team. 2020. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.
Wickham, Hadley. 2017a. Reshape2: Flexibly Reshape Data: A Reboot of the Reshape Package. https://CRAN.R-project.org/package=reshape2.
———. 2017b. Tidyverse: Easily Install and Load the ’Tidyverse’. https://CRAN.R-project.org/package=tidyverse.
———. 2019a. Forcats: Tools for Working with Categorical Variables (Factors). https://CRAN.R-project.org/package=forcats.
———. 2019b. Stringr: Simple, Consistent Wrappers for Common String Operations. https://CRAN.R-project.org/package=stringr.
Wickham, Hadley, and Lionel Henry. 2020. Tidyr: Tidy Messy Data. https://CRAN.R-project.org/package=tidyr.
Wickham, Hadley, Winston Chang, Lionel Henry, Thomas Lin Pedersen, Kohske Takahashi, Claus Wilke, Kara Woo, and Hiroaki Yutani. 2019. Ggplot2: Create Elegant Data Visualisations Using the Grammar of Graphics. https://CRAN.R-project.org/package=ggplot2.
Wickham, Hadley, Romain François, Lionel Henry, and Kirill Müller. 2019. Dplyr: A Grammar of Data Manipulation. https://CRAN.R-project.org/package=dplyr.
Wickham, Hadley, Jim Hester, and Romain Francois. 2018. Readr: Read Rectangular Text Data. https://CRAN.R-project.org/package=readr.